package com.fingard.dsp.bank.directbank.cdb01;

import com.fingard.FGBiz;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Description: [DSP_国开行余额处理类]
 * Created on 19/03/07
 *
 * @author <a href="mailto:zhangxw@fingard.com.cn">ross</a>
 * @version 1.0
 * Copyright (c) 2019 FinGard
 */
public class CDB01Balance extends CDB01Base implements IBalance {

    /**
     * 余额查询 STTQ0101
     *
     * @param p_req7506
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {

            while (p_req7506.readRecord()) {//不支持批量查询，逐个查询
                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                CDB01Header tmpHeader = new CDB01Header();
                tmpHeader.TransCode = "STTQ0101";
                tmpHeader.Fseqno = getQryTransID();

                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHead(xmlWriter, tmpHeader);

                xmlWriter.writeElementString("BASE_ACCT_NO", p_req7506.readedDetail.actNum);//账号	String(50)	M
                xmlWriter.writeElementString("ACCT_TYPE", getBankConfig().getMapValueByKey("acctTypeMapping", p_req7506.readedDetail.actNum));//账户类型	String(3)	M	请参考公共代码说明账户类型代码表
                xmlWriter.writeElementString("CCY", convertCurToBank(p_req7506.readedDetail.actCur));//币种	String(3)	M	请参考公共代码说明币种代码表
                xmlWriter.writeElementString("CERTIFICATE_NO", "");//存单号	String(8)	C	查询定期存单时有效
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), false);
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/Packet/Data/Pub/RetCode");
                        String tmpRetMsg = tmpBkReader.getFirstText("/Packet/Data/Pub/RetMsg");
                        if ("0000".equals(tmpRetCode) || "000000".equals(tmpRetCode)) {
                            if (tmpBkReader.read("/Packet/Data/Res/ACCT_ARRAY")) {
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleTextNotNull("BASE_ACCT_NO");
                                tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
                                tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleTextNotNull("CCY"));
                                tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("AVAILABLE_BAL");//可用余额	String(17,2)	O
                                tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleTextNotNull("LEDGER_BAL");//账面余额	String(17,2)	O
                                tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleTextNotNull("FUNDS_HELD");//冻结资金	String(17,2)	O
                            }
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = tmpRetMsg;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
            }
        } catch (Exception ex) {
            FGBiz.limitMsg.exception(reqHeader.bankCode, ex);
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(ex.getMessage());
            WriteBankLogLn(ex);
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
